<template>
  <div class="sidebar">
    <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="sidebar.collapse"
        :background-color="sidebar.bgColor"
        :text-color="sidebar.textColor"
        router
    >
      <template v-for="item in filteredMenuData">
        <template v-if="item.children">
          <el-sub-menu :index="item.index" :key="item.index" v-permiss="item.id">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.children">
              <a v-if="item.title=='组件'&&subItem.title=='图像识别'" href="http://localhost:63342/GPT-webUI/public/chat.html">
                <el-menu-item>
                  <template #title>知识问答</template>
                </el-menu-item>
              </a>
              <el-sub-menu
                  v-if="subItem.children"
                  :index="subItem.index"
                  :key="subItem.index"
                  v-permiss="item.id"
              >
                <template #title>{{ subItem.title }}</template>
                <el-menu-item
                    v-for="(threeItem, i) in subItem.children"
                    :key="i"
                    :index="threeItem.index"
                >
                  {{ threeItem.title }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.index" v-permiss="item.id">
                {{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index" v-permiss="item.id">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useSidebarStore } from '@/store/sidebar';
import { useRoute } from 'vue-router';
import { usePermissStore } from '@/store/permiss';
import { menuData } from '@/components/menu';

const route = useRoute();
const onRoutes = computed(() => {
  return route.path;
});

const sidebar = useSidebarStore();
const permiss = usePermissStore();

const filteredMenuData = computed(() => {
  const userRole = permiss.getUserRole();
  if (userRole === 'admin') {
    return menuData;
  } else {
    return filterMenuData(menuData);
  }
});

function filterMenuData(menu: any[]): any[] {
  return menu.filter(item => !item.adminOnly).map(item => {
    if (item.children) {
      item.children = filterMenuData(item.children);
    }
    return item;
  });
}
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
    width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}

.sidebar-el-menu {
    min-height: 100%;
}
</style>
